package com.zbmsg.utils.id;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class OrderIdUtil {

	private static final int SUB_USERID_LENGTH = 7;

	/**
	 * 订单号生成策略: 日期(yyMMdd)+订单类型 + 两位随机数 + 用户id(后七位)+ 三位随机数
	 * 
	 * @param orderType
	 * @param userId
	 * @return
	 */
	public static String generateOrderId(int orderType, int userId) {
		String userIdStr = String.valueOf(userId);
		StringBuilder stringBuilder = new StringBuilder();
		Date date = new Date();
		String format = new SimpleDateFormat("yyMMdd").format(date);
		Random random = new Random();
		int userIdlength = userIdStr.length();
		if (userIdlength > SUB_USERID_LENGTH) {
			userIdStr = userIdStr.substring(userIdlength - SUB_USERID_LENGTH, userIdlength);
		}
		stringBuilder.append(orderType).append(format).append((random.nextInt(90) + 10))
		.append(userIdStr).append((random.nextInt(900) + 100));
		return stringBuilder.toString();
	}
}
